import 'dart:convert';
import 'dart:io' as io;
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:t3_data_service/data_provider/map_sctx/t3_map_sctx_provider.dart';
import 'package:t3_map/t3_map_plugin/map/core/t3_location.dart';
import 'package:t3_map/t3_map_plugin/poi/poi_manager/t3_poi_data_service.dart';
import 'package:t3_map/t3_map_plugin/route/route_manager/t3_map_route_manager.dart';
import 'package:t3_map/t3_map_plugin/route/route_manager/t3_route_data_service.dart';
import 'package:t3_network/t3_network.dart';

void main() {

  const MethodChannel channel = MethodChannel('t3_data_service');
  TestWidgetsFlutterBinding.ensureInitialized();
  ///解决使用TestWidgetsFlutterBinding导致网络请求被覆写的问题：https://github.com/flutter/flutter/issues/48050
  io.HttpOverrides.global  = null;
  
  setUp(() async {
    TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockMethodCallHandler(channel, (MethodCall methodCall) async{

      if (methodCall.method == 'getMapData') {
        return jsonEncode({"currentMapType" : "2"});
      }
      return null;
    });

    T3Network.globalHeaders = () {
      T3HTTPHeaders headers = T3HTTPHeaders();
      headers.accept = "application/json";
      headers.contentType = "application/json";

      // 应用信息
      headers.appid = "zcde31a34eb511e89b9c0242ac1e0015";
      headers.clientType = T3ClientType.passenger;
      headers.grayVersion = "P_i_4.1.6";
      headers.grayBuild = "961";

      //添加错误表示，如果设备信息获取不到，统一添加-1作为错误标识
      headers.deviceToken = "63CFD90AAC9A4A6793AF98087AA54960";
      headers.riskDeviceToken = "66f64a7cYIuVziLHefW82vZNkla9Y4ZvRNdGqoA2";

      headers.riskToken = "GW1bSEdVCDsWT2bYvVkVWZYtA+vslUzn/E8kymfEzHrCTgW5wZqMo+8SjRZVQqHm";

      headers.noncestr = (DateTime.now().microsecondsSinceEpoch / 1000).toString();

      headers.sign = headers.noncestr;

      /// 用户实时信息
      headers.token = "d2e57b4b-00a4-40f7-ab9e-1f9c637f7600";

      headers.locationPermission = T3LocationPermission.authorized;

      headers.cityCode = "025";

      headers.adCode = "320115";

      return headers;
    };
  });

  test("步行算路接口：getWalkRouteFromService", () async {

    const origin = T3LocationCoordinate2D(longitude: 116.359406, latitude: 39.910612);
    const destination = T3LocationCoordinate2D(longitude: 116.417165, latitude: 39.91367);
    
    final value = await T3RouteDataService.getWalkRouteFromService(origin: origin, destination: destination);

    debugPrint("object-- $value");
  });

  
   test("逆地理接口：getRegeoFromService", () async {

    const coordinate = T3LocationCoordinate2D(longitude: 116.417165, latitude: 39.91367);
    
    final value = await T3POIDataService.getRegeoFromService(coordinate: coordinate);

    debugPrint("object-- $value");
  });

    test("驾车路线算路接口：getDrivingRoute", () async {

    const origin = T3LocationCoordinate2D(longitude: 116.359406, latitude: 39.910612);
    const destination = T3LocationCoordinate2D(longitude: 116.417165, latitude: 39.91367);
    
    final value = await T3RouteManager.getDrivingRoute(mapType: T3MapType.amap, origin: origin, destination: destination);
    debugPrint("object-- $value");
  });

}